/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package py.edu.ucom.cadira.game.event.war;

import org.jdom.Document;
import org.jdom.Element;

import py.edu.ucom.cadira.game.war.Square;
import py.edu.ucom.cadira.game.war.WarUser;

/**
 * 
 * @author largonet
 */
public class WarEvent extends WarGameEvent {
    private Square casillaGanadora;
    private Square casillaPerdedora;
    private WarUser userWinner;

    public WarEvent() {
		
	}

    public Square getCasillaGanadora() {
		return casillaGanadora;
	}
    public void setCasillaGanadora(Square casillaGanadora) {
		this.casillaGanadora = casillaGanadora;
	}
	public Square getCasillaPerdedora() {
		return casillaPerdedora;
	}
	public void setCasillaPerdedora(Square casillaPerdedora) {
		this.casillaPerdedora = casillaPerdedora;
	}
	public WarUser getUserWinner() {
		return userWinner;
	}
	public void setUserWinner(WarUser userWinner) {
		this.userWinner = userWinner;
	}
	
	public WarEvent(Square casillaGanadora, Square casillaPerdedora, WarUser userWinner) {
        this.casillaGanadora = casillaGanadora;
        this.casillaPerdedora = casillaPerdedora;
        this.userWinner = userWinner;
    }
    public String toString(){
        StringBuffer buff=new StringBuffer();
       buff.append(super.toString());
       buff.append("WarEvent: "+ userWinner.toString());
        return buff.toString();
    }
	@Override
	public void fromXML(Document xml) {
		Element root = xml.getRootElement();
		Element warEventNode = root.getChild("WarEvent");
		Element casillasNode = warEventNode.getChild("casillas");
		Element casillaGanadoraNode = casillasNode.getChild("casillaGanadora");
		casillaGanadora.fromXml(casillaGanadoraNode);
		
		Element casillaPerdedoraNode = casillasNode.getChild("casillaPerdedora");
		casillaPerdedora.fromXml(casillaPerdedoraNode);
		
		Element userNode = warEventNode.getChild("usuario");
		userWinner.fromXml(userNode);
	}
	@Override
	public void toXML(Element root) {
		Element warEventNode = new Element("WarEvent");
		Element casillasNode = new Element("casillas");
		Element casillaGanadoraNode = new Element("casillaGanadora");
		casillaGanadora.toXml(casillaGanadoraNode);
		
		Element casillaPerdedoraNode = new Element("casillaPerdedora");
		casillaPerdedora.toXml(casillaPerdedoraNode);
		
		Element userNode = userWinner.toXml();
		
		casillasNode.addContent(casillaGanadoraNode);
		casillasNode.addContent(casillaPerdedoraNode);
		warEventNode.addContent(casillasNode);
		warEventNode.addContent(userNode);
		
		root.addContent(warEventNode);
	}
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof WarEvent)) {
			return false;
		}
		WarEvent otherObj = (WarEvent) obj;
		if (otherObj.getCasillaGanadora().equals(getCasillaGanadora())) {
			return true;
		}
		return false;
	}
}
